<?php

/*
	任务列表（工作人员）
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['statuss']=$this->work_status;
if(isset($_RQ['type'])){
	$this->session('gtype',$_RQ['type']);
}
$gtype=$this->session('gtype');
$gtypetitle=$this->siyu_type[$gtype];
$tabs=array(
	array('op'=>'list','title'=>$gtypetitle.'-私域运营任务'),
	array('op'=>'edit','title'=>$gtypetitle.'-创建任务'),
);
set_html('tabs',$tabs);
$return['task_status']=$this->task_status;
$return['task_type']=$this->task_type;
$return['ctype']=$this->ctype;
$return['tixin_type']=$this->tixin_type;
$workergroup=pdo_getall('jy_scrm_worker_group',array('uniacid'=>$_SESSION['uniacid']),array('bumenid','title'),'bumenid');
foreach($workergroup as $wv){
	$return['workergroup'][$wv['bumenid']]=$wv['title'];
}
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>3,'gtype'=>$gtype);
	//类型。1表示开发任务，2表示bug修复任务，3表示客户异常问题处理
	if(!empty($_RQ['ctype'])){
		$params['ctype']=intval($_RQ['ctype']);
		$return['params']['ctype']=$_RQ['ctype'];
	}
	//状态。0表示待处理。1表示处理中。2表示处理完毕待审核。3表示审核通过。
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	//任务名称
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrmtask_list',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$row){
		$worker=pdo_get('jy_scrm_worker',array('id'=>$row['workerid'],'uniacid'=>$_SESSION['uniacid']));
		$row['member']=array('mobile'=>$worker['alias'],'avatar'=>$worker['avatar'],'nickname'=>$worker['name']);
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：这里的任务库，1、可以用来公司批量指派给员工；2、也可以让员工自己领取；3、也可以由客户选用任务发起！','list'=>array(
			array('name'=>'title','title'=>'任务名称','type'=>'string'),
			array('name'=>'usernum','title'=>'任务数','type'=>'string'),
			array('name'=>'ctype','title'=>'内容类型','type'=>'radio','options'=>'ctype'),
			array('name'=>'tixin_type','title'=>'提醒类型','type'=>'radio','options'=>'tixin_type'),
			array('name'=>'status','title'=>'任务状态','type'=>'radio','options'=>'task_status'),
			array('name'=>'end_time','title'=>'任务结束时间','type'=>'time'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			//array('type'=>'modal','op'=>'send','title'=>'发任务','show'=>array('receive_type'=>1)),
		)),
		'search'=>array(
			array('name'=>'ctype','width'=>15,'title'=>'内容类型','type'=>'radio','options'=>'ctype'),
			array('name'=>'status','width'=>15,'title'=>'任务状态','type'=>'radio','options'=>'task_status'),
			array('type'=>'string','width'=>15,'name'=>'title','title'=>'任务名称'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择任务！','error');
	}
	$item=pdo_get('jy_scrmtask_list',array('id'=>$id));
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'任务详情',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'look'=>array(
			array('type'=>'string','name'=>'title','title'=>'任务名称'),
			array('type'=>'text','name'=>'content','title'=>'任务内容'),
			array('type'=>'images','name'=>'image','title'=>'截图'),
		),
	);
	exi($return);
}
//添加编辑任务
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'title'=>trim($post['title']),
			'worker_type'=>intval($post['worker_type']),
			'ctype'=>intval($post['ctype']),
			'content'=>trim($post['content']),
			'image'=>trim($post['image']),
			'images'=>ijson_encode($post['images']),
			'tuwen'=>ijson_encode($post['tuwen']),
			'workergroup'=>trim($post['workergroup']),
			'workers'=>ijson_encode($post['workers']),
			'tixin_type'=>intval($post['tixin_type']),
			'tixin_time'=>strtotime($post['tixin_time']),
			'tixin_hour'=>trim($post['tixin_hour']),
			'end_time'=>strtotime($post['end_time']),
			'status'=>intval($post['status']),
			'tags'=>trim($post['tags']),
			'usernum'=>intval($post['usernum']),
		);
		$id=intval($post['id']);
		if($id){
			pdo_update('jy_scrmtask_list',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		}else{
			$data['createtime']=TIMESTAMP;
			$data['type']=3;
			$data['uniacid']=$_SESSION['uniacid'];
			$data['gtype']=$gtype;
			pdo_insert('jy_scrmtask_list',$data);
			$id=pdo_insertid();
		}
		if(!$post['tixin_type'] && $post['status']==1){
			$this->model('model')->send_task($id);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrmtask_list',array('id'=>$id));
		$item['tixin_time'] =$item['tixin_time']?date('Y-m-d H:i',$item['tixin_time']):'';
		$item['end_time'] =$item['end_time']?date('Y-m-d H:i',$item['end_time']):'';
	}else{
		$item=array('worker_type'=>0);
	}
	$item['ctype']=$item['ctype']?:0;
	$item['gtype']=$gtype;
	$item['workers']=$item['workers']?json_decode($item['workers'],true):array();
	$item['images']=$item['images']?json_decode($item['images'],true):array();
	$item['tuwen']=$item['tuwen']?json_decode($item['tuwen'],true):array();
	$return['groups']=pdo_getall('jy_workliu_workgroup',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['item']=$item;
	$return['htmls'][]=array(
		'type'=>'look',
		'width'=>'350px',
		'flex'=>1,
		'look'=>array(
				array('type'=>'msg_send','title'=>'title','text'=>'content','textimg'=>'image'),
		),
	);
	$return['htmls'][]=array(
		'type'=>'edit',
		'edit'=>array(
			array('name'=>'title','title'=>'任务名称','type'=>'string'),
			array('name'=>'worker_type','title'=>'员工类型','type'=>'radio','options'=>'worker_type'),
			array('name'=>'workergroup','title'=>'适合部门','type'=>'checkbox','options'=>'workergroup','show'=>array('worker_type'=>0)),
			array('type'=>'choose_many','title'=>'选择员工','name'=>'workers','action'=>'jy_scrm/worker.get_worker/many/','show'=>array('worker_type'=>1)),
			array('name'=>'usernum','title'=>'净添加好友数','type'=>'unitstring','unit'=>'人','show'=>array('gtype'=>0)),
			array('name'=>'usernum','title'=>'净邀请入群数','type'=>'unitstring','unit'=>'个','show'=>array('gtype'=>1)),
			array('name'=>'end_time','title'=>'任务结束时间','type'=>'time'),
			array('name'=>'ctype','title'=>'内容类型','type'=>'radio','options'=>'ctype'),
			array('name'=>'content','title'=>'群发文本','type'=>'text_wx','show'=>array('ctype'=>0)),
			array('name'=>'image','title'=>'群发图片','type'=>'image','show'=>array('ctype'=>1)),
			array('name'=>'tixin_type','title'=>'提醒时间','type'=>'radio','options'=>'tixin_type'),
			array('name'=>'tixin_hour','title'=>'每天提醒时间','type'=>'time_onehour','show'=>array('tixin_type'=>1)),
			array('name'=>'tags','title'=>'客户标签','type'=>'checkbox_select','options'=>'tags','option_value'=>'tag_id'),
			array('name'=>'status','title'=>'任务状态','type'=>'radio','options'=>'task_status'),
		),
	);
	$return['worker_type']=$this->worker_type;
	$return['tags']=pdo_getall('jy_scrm_member_group_tag',array('uniacid'=>$_SESSION['uniacid']),array('tag_id','title'),'id');
	exi($return);
}
if($_RQ['op']=='send'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择任务！','error');
		}
		$item=pdo_get('jy_scrmtask_list',array('id'=>$id));
		$workerids=str_array($post['workerids']);
		if(!count($workerids)){
			exi('请先选择员工！','error');
		}
		foreach($workerids as $wv){
			if(!$wv){
				continue;
			}
			$data=array(
				'title'=>trim($item['title']),
				'groupid'=>intval($item['groupid']),
				'content'=>trim($item['content']),
				'image'=>ijson_encode($item['image']),
				'tags'=>trim($item['tags']),
				'credit'=>intval($item['credit']),
				'type'=>0,
				'jiaji'=>intval($item['jiaji']),
				'status'=>1,
				'receive_type'=>1,
				'receive_time'=>TIMESTAMP,
				'workerid'=>intval($wv),
			);
			$data['createtime']=TIMESTAMP;
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_scrmtask_list',$data);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择任务！','error');
	}
	$item=pdo_get('jy_scrmtask_list',array('id'=>$id),array('id','title'));
	if(!$item){
		exi('指定任务不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'指派员工处理任务',
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'任务名称','read'=>true),
			array('type'=>'checkbox','title'=>'选择员工','name'=>'workerids','options'=>'workers'),
		),
	);
	$return['workers']=pdo_getall('jy_scrm_worker',array('uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	foreach($return['workers'] as &$wv){
		$wv=$wv['name'];
	}
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrmtask_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}